﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MODULE_TIENICH
{
    public partial class frm_timkiemchuhang : Form
    {
        tienichDataContext context = cls_tienich.getcontext();
        private string smachuhang;
        //private string stenchuhang;
        public string _smachuhang
        {
            get { return this.txtmachuhang.Text; }
            set { this.txtmachuhang.Text = value; }
        }
       
        int currenrowindex;
        public frm_timkiemchuhang()
        {
            InitializeComponent();
        }
       
        private void frm_timkiemchuhang_Load(object sender, EventArgs e)
        {
            

            List<DM_chuhang> chuhangs = context.DM_chuhangs.ToList();
            string machuhang = this.txtmachuhang.Text;

            if (chuhangs.Count > 0)
            {
                grdata.Rows.Clear();
                for (int i = 0; i < chuhangs.Count(); i++)
                {
                    DM_chuhang chuhang = chuhangs.ElementAt(i);
                    grdata.Rows.Add();
                    grdata.Rows[i].Cells["machuhang"].Value = chuhang.machuhang;
                    grdata.Rows[i].Cells["tenchuhang"].Value = chuhang.tenchuhang;
                }
                currenrowindex = 0;
                this.grdata.Rows[currenrowindex].Selected = true;
                this.txtmachuhang.Text = grdata.Rows[currenrowindex].Cells["machuhang"].Value.ToString();

            }
            
        }

        private void frm_timkiemchuhang_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void frm_timkiemchuhang_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void frm_timkiemchuhang_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            
        }

        private void frm_timkiemchuhang_KeyDown(object sender, KeyEventArgs e)
        {
           
        }

        private void grdata_Click(object sender, EventArgs e)
        {
           
            
            
        }
       

        private void grdata_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void grdata_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                
                this.Close();
                
            }
            if (e.KeyCode == Keys.Down)
            {
                try
                {
                    if (currenrowindex < grdata.Rows.Count-1)
                    {
                        currenrowindex = currenrowindex + 1;
                        this.grdata.Rows[currenrowindex].Selected = true;
                        this.txtmachuhang.Text = grdata.Rows[currenrowindex].Cells["machuhang"].Value.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            if (e.KeyCode == Keys.Up)
            {
                try
                {
                    if (currenrowindex>0)
                    {
                        currenrowindex = currenrowindex - 1;
                        this.grdata.Rows[currenrowindex].Selected = true;
                        this.txtmachuhang.Text = grdata.Rows[currenrowindex].Cells["machuhang"].Value.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            //if (e.KeyCode == Keys.Control &&  Keys.F)
            //{
            //    MessageBox.Show("A");
            //}
        }

        private void grdata_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            
            //try
            //{
            //    currenrowindex = e.RowIndex;
            //    this.grdata.Rows[currenrowindex].Selected = true;
            //    this.txtmachuhang.Text = grdata.Rows[currenrowindex].Cells["machuhang"].Value.ToString();
            //}
            //catch (Exception ex)
            //{
            //    //MessageBox.Show(ex.Message);
            //}
            
        }

        private void grdata_RowEnter(object sender, DataGridViewCellEventArgs e)
        {



            //try
            //{
            //    currenrowindex = e.RowIndex;
            //    this.grdata.Rows[currenrowindex].Selected = true;
            //    this.txtmachuhang.Text = grdata.Rows[currenrowindex].Cells["machuhang"].Value.ToString();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
           
           
        }

        private void grdata_Leave(object sender, EventArgs e)
        {
            

        }

        private void grdata_Enter(object sender, EventArgs e)
        {
            
        }

        private void grdata_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            //try
            //{
            //    currenrowindex = e.RowIndex;
            //    this.grdata.Rows[currenrowindex].Selected = true;
            //    this.txtmachuhang.Text = grdata.Rows[currenrowindex].Cells["machuhang"].Value.ToString();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
        }

        private void grdata_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                currenrowindex = e.RowIndex;
                this.grdata.Rows[currenrowindex].Selected = true;
                this.txtmachuhang.Text = grdata.Rows[currenrowindex].Cells["machuhang"].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtkeyword_TextChanged(object sender, EventArgs e)
        {
            string keyword = txtkeyword.Text;
            IEnumerable<DM_chuhang> chuhangs =from c in  context.DM_chuhangs where c.tenchuhang.Contains(keyword) select c;
            string machuhang = this.txtmachuhang.Text;

            if (chuhangs.Count() > 0)
            {
                grdata.Rows.Clear();
                for (int i = 0; i < chuhangs.Count(); i++)
                {
                    DM_chuhang chuhang = chuhangs.ElementAt(i);
                    grdata.Rows.Add();
                    grdata.Rows[i].Cells["machuhang"].Value = chuhang.machuhang;
                    grdata.Rows[i].Cells["tenchuhang"].Value = chuhang.tenchuhang;
                }
                currenrowindex = 0;
                this.grdata.Rows[currenrowindex].Selected = true;
                this.txtmachuhang.Text = grdata.Rows[currenrowindex].Cells["machuhang"].Value.ToString();

            }
            
           
        }

        
    }
}
